package com.reconstructme.toggle;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class ReconstructMe extends Activity {

	public static final String PREFS_NAME = "ReconstructMe";
	
	public static final String IP_TAG = "IP";
	public static final String IP_DEFAULT = "127.0.0.1";
	
	public static final String PORT_TAG = "Port";
	public static final int PORT_DEFAULT = 6666;
	
	public static final String MESSAGE_TAG = "MESSAGE";
	public static final String MESSAGE_DEFAULT = "MY MESSAGE\0";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reconstruct_me);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_reconstruct_me, menu);
        
        // Add listener for settings click event and open settings activity
        MenuItem i = menu.findItem(R.id.menu_settings);
        i.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
			public boolean onMenuItemClick(MenuItem item) {
				Intent in = new Intent
				(
						ReconstructMe.this,
						Settings.class
				);
				startActivity(in);
				return false;
			}
		});
        
        return true;
    }
    
	public void toggle_btn_clicked(View view) {
    	// get settings
        SharedPreferences settings = getSharedPreferences(ReconstructMe.PREFS_NAME, MODE_PRIVATE);
        String ip_restored = settings.getString(IP_TAG, IP_DEFAULT);
        int port_restored = settings.getInt(PORT_TAG, PORT_DEFAULT);
        String msg_restored = settings.getString(MESSAGE_TAG, MESSAGE_DEFAULT);
        
		new UDPAsyncTask().execute(msg_restored, ip_restored, String.valueOf(port_restored));
    }
    
}
